package com.itheima.reggie.service.impl;

import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dao.DishFlavorDao;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.pojo.Page;
import com.itheima.reggie.service.DishService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import java.util.Random;


@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishDao dishDao;

    @Autowired
    private DishFlavorDao dishFlavorDao;


    @Override
    @Cacheable(value = "dishCache", key = "'dish_'+ #pageNum")
    public Page<DishDto> findByPage(Integer pageNum, Integer pageSize, String name) {
        List<DishDto> dishes = dishDao.findAll(name);
        return new Page<>(dishes);
    }

    @Override
    @SneakyThrows
    @CacheEvict(value = "dishCache", allEntries = true)
    public void saveWithFlavor(DishDto dishDto) {
        //1.添加菜品信息
        //1.1 填充code字段，没有用到的，填充6个0，字符串类型
        dishDto.setCode("000000");
        //1.2 随机生成一个排序字段，生成一个正整数
        dishDto.setSort(Math.abs(new Random().nextInt()));
        //1.3 添加到菜品表中
        dishDao.save(dishDto);

        //2.添加多条口味信息
        //2.1 获取多条口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            //2.2 需要设置口味的外键，新添加的菜品主键值
            flavor.setDishId(dishDto.getId());
            //2.3 添加到口味表中
            dishFlavorDao.save(flavor);
        }
    }

    @Override
    @Cacheable(value = "dishCache", key = "#id")
    public DishDto findById(Long id) {
        Dish dish = dishDao.findById(id);
        List<DishFlavor> dishFlavorList = dishFlavorDao.findByDishId(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    @Override
    @CacheEvict(value = "dishCache", allEntries = true)
    public Integer updateStatus(Integer status, Long[] ids) {
        int count = 0;
        for (int i = 0; i < ids.length; i++) {
            dishDao.updateStatus(status, ids[i]);
            count++;
        }
        return count;
    }

    @Override
    @Cacheable(value = "dishCache", key = "#dish+'_'+#categoryId")
    public List<DishDto> findByCategoryId(Long categoryId, String name, Integer status) {
        List<DishDto> dishList = dishDao.findByCategoryId(categoryId, name, status);
        return dishList;
    }


    @Override
    @CacheEvict(value = "dishCache", allEntries = true)
    public void updateWithFlavor(DishDto dishDto) {
        dishDao.updateById(dishDto);
        dishFlavorDao.deleteByDishId(dishDto.getId());
        //3. 获取所有口味信息，重新添加
        List<DishFlavor> flavors = dishDto.getFlavors();
        //4. 循环保存多条口味信息
        flavors.forEach(flavor -> {
            //4.1 设置口味的id为dishDto的id
            flavor.setDishId(dishDto.getId());
            //4.2 调用添加口味信息自动填充通用属性
            dishFlavorDao.save(flavor);
        });
    }

    @Override
    @Transactional
    @CacheEvict(value = "dishCache", allEntries = true)
    public int deleteByIds(Long[] ids) {
        int count = 0;
        for (Long id : ids) {
            Dish dish = dishDao.findById(id);
            if (dish.getStatus() == 1) {
                throw new CustomException("商品启用中，不能停售");
            }
            dishFlavorDao.deleteByDishId(id);
            dishDao.deleteByid(id);
        }
        return count;
    }
}
